<?php
namespace Seller\Model;
use Think\Model\RelationModel;
class OrderModel extends RelationModel
{
    protected $_link = array(
        //代理表
        'agent' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Agent',
            'foreign_key' => 'uid',
            'mapping_fields' => 'agent_name,mobile',
            'as_fields' => 'agent_name',
        ),

        //收货地址表
        'address' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'AgentAddress',
            'foreign_key' => 'address_id',
            'as_fields' => 'province,city,area,detail_address,consignee:consignee2,mobile'
        )
    );

    //格式化订单
    public function format($order) {
        foreach ($order as $key => $value) {
            $order[$key]['time'] = date('Y-m-d H:i:s', $order[$key]['time']);
            switch ($order[$key]['status']) {
                case '-1' :
                    $order[$key]['status'] = '已取消';
                    break;
                case '3' :
                    $order[$key]['status'] = '已完成';
                    break;
                default:
                    $order[$key]['status'] = '进行中';
                    break;
            }
            $order[$key]['is_pay'] = ($order[$key]['is_pay'] == 1) ? '已付款' : '未付款';
            $order[$key]['is_deliver'] = ($order[$key]['is_deliver'] == -1) ? '未发货' : '已发货';
        }
        return $order;
    }




    //订单对应多个商品
    public function orderGood($where, $limit='0,3') {
        //订单列表
        $order = M('order')->field('id, order_id,status,is_deliver,uid,is_pay,price,discount,freight_price')
            ->where($where)->order('id DESC')->limit($limit)->select();

        //echo M('order')->getLastSql();


        //拼接订单的多件商品
        foreach ($order as $key=>$value) {
            $order[$key]['order_good'] = M('orderGood')->alias('a')
                ->field('a.order_id,a.num,b.g_name,b.img,a.good_id')
                ->join('LEFT JOIN __GOOD__ b ON a.good_id=b.id')
                ->where( array('a.order_id'=>$value['id']) )
                ->select();
            //计算商品的总个数
            foreach ($order[$key]['order_good'] as $v) {$order[$key]['good_num'] += $v['num'];}
        }
        return $order;
    }

    //订单流拼接多个商品
    public function orderGoodThrough($map, $limit='0,3') {
        $order = M('orderThrough a')->field('b.id,b.order_id,b.status,b.uid,b.price,b.freight_price,b.is_deliver,b.is_pay,a.uid as uuid,a.state')->join('LEFT JOIN __ORDER__ as b ON b.id=a.order_id')->where($map)->order('b.id DESC')->limit($limit)->select();
        //拼接订单的商品
        foreach ($order as $key=>$value) {
            $order[$key]['order_good'] = M('orderGood')->alias('a')
                ->field('a.order_id,a.num,b.g_name,b.img,b.brand_id,a.good_id')
                ->join('LEFT JOIN __GOOD__ b ON a.good_id=b.id')
                ->where( array('a.order_id'=>$value['id']) )
                ->select();
            //计算商品的总个数
            foreach ($order[$key]['order_good'] as $v) {$order[$key]['good_num'] += $v['num'];}
            //计算订单的总价格
            $order[$key]['total_price'] = top_order_price($value['id'],$value['uuid']);//
        }
        return $order;
    }










}


